package com.ninong.ker.admin.sevice.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ninong.ker.admin.service.dto.DicDTO;
import com.ninong.ker.common.auth.SecurityAdmin;
import com.ninong.ker.common.tools.PropertyUtil;
import com.ninong.ker.common.tools.SystemConstant;
import com.ninong.ker.common.user.OnlineUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ninong.ker.admin.domain.Dic;
import com.ninong.ker.admin.domain.DicDetail;
import com.ninong.ker.admin.mapper.DicDetailMapper;
import com.ninong.ker.admin.mapper.DicMapper;
import com.ninong.ker.admin.sevice.DicService;
import com.ninong.ker.common.exception.CmdException;
import com.ninong.ker.common.tools.Constants;

/**
 * @author cmd
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class DicServiceImpl extends ServiceImpl<DicMapper, Dic> implements DicService {

    @Autowired
    private SecurityAdmin securityAdmin;
    @Autowired
    private DicDetailMapper dicDetailMapper;

    @Override
    public PageInfo<Dic> listPage(DicDTO dto) {
        OnlineUser onlineUser = securityAdmin.currentUser();
        String tenantId = onlineUser.getTenantId();
        PageHelper.startPage(dto.getCurrentPage(), dto.getPageSize());
        LambdaQueryWrapper<Dic> query = new LambdaQueryWrapper<>();
        query.eq(Dic::getTenantId, tenantId);
        if (!StringUtils.equalsIgnoreCase(SystemConstant.ROOT_PARENT_ID, tenantId)) {
            query.eq(Dic::getHasSystem, Constants.DEFAULT_NO);
        }
        if (!StringUtils.isEmpty(dto.getCode())) {
            query.like(Dic::getCode, dto.getCode());
        }
        if (!StringUtils.isEmpty(dto.getValue())) {
            query.like(Dic::getValue, dto.getValue());
        }
        List<Dic> lists = baseMapper.selectList(query);
        return new PageInfo<>(lists);
    }

    @Override
    public void delete(String[] ids) {
        List<String> idList = Arrays.asList(ids);
        idList.forEach(id -> {
            Long count = dicDetailMapper.selectCount(new LambdaQueryWrapper<DicDetail>().eq(DicDetail::getDicId, id));
            if (count > Constants.ZERO) {
                String mess = PropertyUtil.getProperty("DIC_DETAIL_EXIST");
                throw new CmdException(100,mess);
            }

        });
        baseMapper.deleteBatchIds(idList);
    }

    @Override
    public void create(Dic entity) {
        OnlineUser onlineUser = securityAdmin.currentUser();
        String tenantId = onlineUser.getTenantId();
        Long count;
        if (StringUtils.isEmpty(entity.getId())) {
            count = baseMapper.selectCount(new LambdaQueryWrapper<Dic>().eq(Dic::getCode, entity.getCode()));
            if (count > Constants.ZERO) {
                String mess = PropertyUtil.getProperty("DIC_CODE_REPEAT");
                throw new CmdException(100,mess);
            }
        } else {
            count = baseMapper
                    .selectCount(new LambdaQueryWrapper<Dic>().ne(Dic::getId, entity.getId()).eq(Dic::getCode, entity.getCode()));
            if (count > Constants.ZERO) {
                String mess = PropertyUtil.getProperty("DIC_CODE_REPEAT");
                throw new CmdException(100,mess);
            }

        }
        if (StringUtils.equalsIgnoreCase(SystemConstant.ROOT_PARENT_ID, tenantId)) {
            entity.setHasSystem(true);
        } else {
            entity.setHasSystem(false);
        }
        entity.setTenantId(tenantId);
        saveOrUpdate(entity);
    }

    @Override
    public Dic get(String id) {
        Dic entity = baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(entity)) {
            entity = new Dic();
        }
        return entity;
    }

}
